using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;
namespace BMC.ARSystem
{
	public class CSVDataExporter : DataExporter
	{
		protected override object CreateWriter(string filePath, Encoding encoding, bool isAppendMode)
		{
			return new StreamWriter(filePath, isAppendMode, encoding);
		}
		protected override void CloseWriter(object writer)
		{
			((StreamWriter)writer).Close();
		}
		protected override void WriteHeader(string formName, string serverCharSet, ArrayList fieldNames, Hashtable fieldsMap, object writer)
		{
			StringBuilder stringBuilder = new StringBuilder();
			int i = 0;
			int count = fieldNames.get_Count();
			while (i < count)
			{
				string text = (string)fieldNames.get_Item(i);
				if (((Field)fieldsMap.get_Item(text)).Id == 15u)
				{
					stringBuilder.AppendFormat("{0}{1}", Util.QuoteStringForCSV(DataExporter.LABEL_STATUS_HISTORY_DEFAULT_LABEL), (i + 1 == count) ? "" : ",");
				}
				else
				{
					stringBuilder.AppendFormat("{0}{1}", Util.QuoteStringForCSV(text), (i + 1 == count) ? "" : ",");
				}
				i++;
			}
			StreamWriter streamWriter = (StreamWriter)writer;
			streamWriter.WriteLine(stringBuilder.ToString());
		}
		protected override void WriteRecord(Server server, object writer, string formName, ArrayList fieldNames, Hashtable fieldsMap, EntryFieldValue entry, string attachValAbsFolder, string attachValRelFolder, ref bool hasAttachVal, ref int attachValCount, ref uint numExported, ref uint numFailed, ref uint numSkipped)
		{
			StringBuilder stringBuilder = new StringBuilder();
			IEnumerator enumerator = fieldNames.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					string text = (string)enumerator.get_Current();
					Field field = fieldsMap.get_Item(text) as Field;
					object obj = entry.FieldValues[field.Id];
					uint dataType = field.DataType;
					string val = "";
					if (field.Id == 102u || field.Id == 123u)
					{
						val = server.SecondOperationTime(obj, field.Id);
					}
					else
					{
						if (obj != null && obj != DBNull.Value && obj != Missing.Value)
						{
							if (field.Id == 15u)
							{
								StatusHistory statusHistory = obj as StatusHistory;
								if (statusHistory != null)
								{
									val = statusHistory.Encode();
								}
							}
							else
							{
								if (field is AttachmentField && obj is Attachment)
								{
									hasAttachVal = true;
									Util.EnsureDirExists(attachValAbsFolder);
									string text2 = ((Attachment)obj).Name;
									string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text2);
									string extension = Path.GetExtension(text2);
									string text3;
									text2 = Path.Combine(attachValAbsFolder, text3 = string.Format("{0}_{1}{2}", fileNameWithoutExtension, attachValCount, extension));
									server.GetEntryBLOB(formName, entry.EntryId, field.Id, text2);
									DataExporter.ExportAttachmentValueEventArgs e = new DataExporter.ExportAttachmentValueEventArgs(numExported, entry, field.Id, attachValAbsFolder, text3);
									this.OnExportAttachmentValueEvent(e);
									val = string.Format("1 {0}", Path.Combine(attachValRelFolder, text3));
									attachValCount++;
								}
								else
								{
									ARXDataTypeWriter.CreateWriter(dataType);
									val = obj.ToString();
								}
							}
						}
					}
					stringBuilder.AppendFormat("{0}{1}", Util.QuoteStringForCSV(val), text.Equals(fieldNames.get_Item(fieldNames.get_Count() - 1)) ? "" : ",");
				}
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
			StreamWriter streamWriter = (StreamWriter)writer;
			streamWriter.WriteLine(stringBuilder.ToString());
		}
		protected override void WriteFooter(object writer)
		{
		}
	}
}
